package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.common.response.JsonResult;
import cn.tedu._04weibo.common.response.StatusCode;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboDTO;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.vo.UserVO;
import cn.tedu._04weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._04weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * RestController: Controller注解 + ResponseBody注解
 */
@Validated
@Api(tags = "02.微博模块")
@RestController
@RequestMapping("v1/weibo/")
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;

    /**
     * 发布微博功能
     * @param weiboDTO
     * @param session
     * @return
     */
    @ApiOperation(value = "发布微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        /*
            1.校验用户登录状态[session]
            2.未登录状态：return 2;
            3.登录状态：在微博表中插入1条数据[接口,实体类]
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){//未登录
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //实例化、复制属性、调用接口方法
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);

        return JsonResult.ok();
    }

    /**
     * 微博首页列表展示
     */
    @ApiOperation(value = "微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndex();

        return JsonResult.ok(weiboIndexVOS);
    }

    /**
     * 微博详情页展示
     * 1.ApiImplicitParam注解：对声明参数方式接收的参数进行描述;
     *   name: 指定参数名称;
     *   value:指定参数描述;
     *   dataType:指定参数类型,必须指定,默认为string;
     */
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "微博编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string")
    })
    @ApiOperation(value = "微博详情页")
    @GetMapping("selectById")
    // String username参数仅仅是为了做Knife4j注解的测试,没有其他用途
    public JsonResult selectById(@Range(min = 200, max = 500, message = "ID值必须在200-500之间") @RequestParam int id, String username){
        if (id <= 0){
            throw new IllegalArgumentException("ID值无效");
        }
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);

        return JsonResult.ok(weiboDetailVO);
    }

}







